from PyQt5 import QtCore, QtGui

class HighlightingRule:
    '''
    定义高亮规则结构
    '''

    def __init__(self, pattern1, format1):
        self.pattern = pattern1
        self.format = format1


class MyHighLighter(QtGui.QSyntaxHighlighter):
    '''
    设置语法高亮
    '''

    def __init__(self, parent):
        QtGui.QSyntaxHighlighter.__init__(self, parent)
        self.parent = parent
        self.highlightingRules = []

        R_Instruction = QtGui.QTextCharFormat()
        R_Instruction.setForeground(QtCore.Qt.magenta)
        R_Instruction.setFontWeight(QtGui.QFont.Bold)
        # R型指令，共17条
        R_Instructions = list(['add', 'addu', 'sub',
                               'subu', 'and', 'or',
                               'xor', 'nor', 'slt',
                               'sltu', 'sll', 'srl',
                               'sra', 'sllv', 'srlv',
                               'srav', 'jr'])
        for instruction in R_Instructions:
            pattern = QtCore.QRegExp('\\b' + instruction + '\\b')
            rule = HighlightingRule(pattern, R_Instruction)
            self.highlightingRules.append(rule)

        I_Instruction = QtGui.QTextCharFormat()
        I_Instruction.setForeground(QtCore.Qt.magenta)
        I_Instruction.setFontWeight(QtGui.QFont.Bold)
        # I型指令，共12条
        I_Instructions = list(['addi', 'addiu', 'andi',
                               'ori', 'xori', 'lui',
                               'lw', 'sw', 'beq',
                               'bne', 'slti', 'sltiu'])
        for instruction in I_Instructions:
            pattern = QtCore.QRegExp('\\b' + instruction + '\\b')
            rule = HighlightingRule(pattern, I_Instruction)
            self.highlightingRules.append(rule)
        
        J_Instruction = QtGui.QTextCharFormat()
        J_Instruction.setForeground(QtCore.Qt.magenta)
        J_Instruction.setFontWeight(QtGui.QFont.Bold)
        # J型指令，共2条
        J_Instructions = list(['j', 'jal'])
        for instruction in J_Instructions:
            pattern = QtCore.QRegExp('\\b' + instruction + '\\b')
            rule = HighlightingRule(pattern, J_Instruction)
            self.highlightingRules.append(rule)
                                       

        # reservedClasses = QtGui.QTextCharFormat()
        # reservedClasses.setForeground(QtCore.Qt.darkRed)
        # reservedClasses.setFontWeight(QtGui.QFont.Bold)
        # keywords = list(["array", "character", "complex",
        #                  "data.frame", "double", "factor",
        #                  "function", "integer", "list",
        #                  "logical", "matrix", "numeric",
        #                  "vector"])
        # for word in keywords:
        #     pattern = QtCore.QRegExp('\\b' + word + '\\b')
        #     rule = HighlightingRule(pattern, reservedClasses)
        #     self.highlightingRules.append(rule)

        #识别16进制数
        Hexadecimal = QtGui.QTextCharFormat()
        pattern = QtCore.QRegExp('[0-9a-fA-F]{8}')
        Hexadecimal.setForeground(QtCore.Qt.red)
        Hexadecimal.setFontWeight(QtGui.QFont.Bold)
        rule = HighlightingRule(pattern, Hexadecimal)
        self.highlightingRules.append(rule)

        # assignmentOperator = QtGui.QTextCharFormat()
        # pattern = QtCore.QRegExp('(<){1,2}-')
        # assignmentOperator.setForeground(QtCore.Qt.green)
        # assignmentOperator.setFontWeight(QtGui.QFont.Bold)
        # rule = HighlightingRule(pattern, assignmentOperator)
        # self.highlightingRules.append(rule)

        # number = QtGui.QTextCharFormat()
        # pattern = QtCore.QRegExp("[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?")
        # pattern.setMinimal(True)
        # number.setForeground(Qt.cyan)
        # rule = HighlightingRule(pattern, number)
        # self.highlightingRules.append(rule)

    def highlightBlock(self, text):
        '''
        redefine highlightBlock函数，添加新的高亮规则
        '''
        for rule in self.highlightingRules:
            expression = QtCore.QRegExp(rule.pattern)
            index = expression.indexIn(text)
            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, rule.format)
                #index = text.index(expression, index + length)
                index = expression.indexIn(text, index + length)
        self.setCurrentBlockState(0)
